var GlobalErrorListener = require('./GlobalErrorListener');
var Event = require('./Event');
var Observer = require('./Observer');
var ClassList = require('./ClassList');
var TextNode = require('./TextNode');
var FakeDomElement = require('./FakeDomElement');
var globalOptions = require('./globalOptions');
var globalState = require('./globalState');
var Element = function (e) {
    h(this, e || null);
};
Element.prototype = Object.create(Object.prototype, {
    constructor: {
        value: Element,
        writable: true,
        configurable: true
    },
    id: {
        get: function () {
            return this.__id;
        },
        set: function (e) {
            var t = String(e);
            if (this.__id !== t) {
                this.__id = t;
                if (this.ownerShadowRoot) {
                    var i = this.ownerShadowRoot.__wxHost;
                    i.__idCacheDirty = true;
                    this.__domElement && i.__componentOptions.writeIdToDOM && (i.__idPrefix ? this.__domElement.id = i.__idPrefix + '--' + t : this.__domElement.id = t);
                }
                globalOptions.writeExtraInfoToAttr && this.__domElement && this.__domElement.setAttribute('exparser:info-attr-id', t);
                (this.__propObservers && !this.__propObservers.empty || this.__subtreeObserversCount) && Observer._callObservers(this, '__propObservers', {
                    type: 'properties',
                    target: this,
                    propertyName: 'id'
                });
            }
        },
        configurable: true
    },
    slot: {
        get: function () {
            return this.__slot;
        },
        set: function (e) {
            e = String(e);
            this.__slot !== e && (this.__inheritSlots ? GlobalErrorListener.hasGlobalWarningListeners() && GlobalErrorListener.triggerWarning('slots-inherited nodes do not support "slot" attribute (in component "' + this.is + '").') : (this.__slot = e, m(this), (this.__propObservers && !this.__propObservers.empty || this.__subtreeObserversCount) && Observer._callObservers(this, '__propObservers', {
                type: 'properties',
                target: this,
                propertyName: 'slot'
            })));
        },
        configurable: true
    },
    attributes: {
        get: function () {
            var e = [];
            if (!this.__attributes)
                return e;
            for (var t in this.__attributes)
                e.push({
                    name: t,
                    value: this.__attributes[t]
                });
            return e;
        },
        set: function () {
        },
        configurable: true
    },
    class: {
        get: function () {
            return this.classList.getClassNames();
        },
        set: function (e) {
            this.classList && this.classList.setClassNames(e);
            (this.__propObservers && !this.__propObservers.empty || this.__subtreeObserversCount) && Observer._callObservers(this, '__propObservers', {
                type: 'properties',
                target: this,
                propertyName: 'class'
            });
        },
        configurable: true
    },
    style: {
        get: function () {
            return this.__domElement ? this.__domElement.style : null;
        },
        set: function (e) {
            return B.call(this, e);
        },
        configurable: true
    }
});
Event._setElementSystem(Element);
ClassList._setElementSystem(Element);
var h = Element.initialize = function (e, t, i) {
    e.__id = '';
    e.__slot = '';
    e.__virtual = false;
    e.__inheritSlots = false;
    e.__attributes = null;
    e.__marks = null;
    e.__attached = false;
    e.parentNode = null;
    e.childNodes = [];
    e.ownerShadowRoot = i;
    e.__wxSlotParent = null;
    e.__wxSlotChildren = e.childNodes;
    e.__subtreeObserversCount = 0;
    e.classList = null;
    e.__styleSegments = [];
    e.__methodCaller = e;
    e.__relationHandler = null;
    e.__backendExtracted = globalState.backendExtractedDefault;
    e.__domElement = t;
    e.$$ = t;
    t && (t.__wxElement = e);
};
Element._clone = function (e, t, i, n) {
    e.__id = t.__id;
    e.__slot = t.__slot;
    e.__virtual = t.__virtual;
    e.__inheritSlots = t.__inheritSlots;
    e.__marks = t.__marks ? {} : null;
    if (t.__marks)
        for (var a in t.__marks)
            e.__marks[a] = t.__marks[a];
    for (var r in (e.__attributes = Object.create(null), t.__attributes))
        e.__attributes[r] = t.__attributes[r];
    e.__attached = false;
    e.parentNode = null;
    e.childNodes = [];
    e.ownerShadowRoot = n;
    e.__wxSlotParent = null;
    e.__wxSlotChildren = e.childNodes;
    e.__subtreeObserversCount = 0;
    e.classList = null;
    e.__styleSegments = [];
    e.__methodCaller = e;
    e.__relationHandler = null;
    e.__backendExtracted = globalState.backendExtractedDefault;
    e.__domElement = i;
    e.$$ = i;
    i && (i.__wxElement = e);
};
var u = function (e) {
    if (!e.parentNode || e.parentNode.__attached) {
        var t = function (e) {
            if (e instanceof Element) {
                e.__attached = true;
                e.__lifeTimeFuncs && e.__lifeTimeFuncs.attached.call(e.__methodCaller, [], e);
                e.__relationHandler && e.__relationHandler('attached');
                e.__attachedObservers && !e.__attachedObservers.empty && Observer._callSingleObserver(e, '__attachedObservers', {
                    type: 'attachStatus',
                    target: e,
                    status: 'attached'
                });
                e.shadowRoot instanceof Element && t(e.shadowRoot);
                var i = e.childNodes;
                for (var n = 0; n < i.length; n++)
                    t(i[n]);
            }
        };
        t(e);
    }
};
var p = function (e) {
    if (e.__attached) {
        var t = function (e) {
            if (e instanceof Element) {
                var i = e.childNodes;
                for (var n = 0; n < i.length; n++)
                    t(i[n]);
                e.shadowRoot instanceof Element && t(e.shadowRoot);
                e.__attached = false;
                e.__lifeTimeFuncs && e.__lifeTimeFuncs.detached.call(e.__methodCaller, [], e);
                e.__relationHandler && e.__relationHandler('detached');
                e.__attachedObservers && !e.__attachedObservers.empty && Observer._callSingleObserver(e, '__attachedObservers', {
                    type: 'attachStatus',
                    target: e,
                    status: 'detached'
                });
            }
        };
        t(e);
    }
};
var g = function (e, t, i) {
    if (e.__childObservers && !e.__childObservers.empty || e.__subtreeObserversCount) {
        var n = null;
        var a = [i];
        n = 'add' === t ? {
            type: 'childList',
            target: e,
            addedNodes: a
        } : 'remove' === t ? {
            type: 'childList',
            target: e,
            removedNodes: a
        } : {
            type: 'childList',
            target: e,
            addedNodes: a,
            removedNodes: a
        };
        Observer._callObservers(e, '__childObservers', n);
    }
};
var _ = function (e, t) {
    if (e instanceof Element && (!t.__wxHost.__idCacheDirty || !t.__slotCacheDirty)) {
        var i = function (e) {
            e.__id && (t.__wxHost.__idCacheDirty = true);
            undefined !== e.__slotName && (t.__slotCacheDirty = true);
            var n = e.childNodes;
            for (var a = 0; a < n.length; a++) {
                var r = n[a];
                r instanceof Element && i(r);
            }
        };
        i(e);
    }
};
Element._updateIdMap = function (e) {
    var t = e.shadowRoot;
    if (e.__idCacheDirty) {
        e.__idCacheDirty = false;
        var i = e.__idCache = Object.create(null);
        var n = function (e) {
            if (e.__id) {
                i[e.__id] || (i[e.__id] = e);
            };
            var t = e.childNodes;
            for (var a = 0; a < t.length; a++)
                t[a] instanceof Element && n(t[a]);
        };
        n(t);
    }
};
var f = function (e) {
    if (e.__hostAssociated && e.__slotCacheDirty) {
        e.__slotCacheDirty = false;
        var t = e.__wxHost;
        var i = null;
        var a = Object.create(null);
        i = undefined !== t.__singleSlot ? { '': t.__singleSlot } : t.__slots;
        var r = function (e) {
            if (undefined !== e.__slotName) {
                if (a[e.__slotName]) {
                    GlobalErrorListener.hasGlobalWarningListeners() && GlobalErrorListener.triggerWarning('More than one slot named "' + e.__slotName + '" are found inside a single component instance (in component "' + t.is + '"). The first one was accepted.');
                } else {
                    a[e.__slotName] = e;
                }
            };
            var i = e.childNodes;
            for (var o = 0; o < i.length; o++)
                i[o] instanceof Element && r(i[o]);
        };
        r(e);
        undefined !== t.__singleSlot ? t.__singleSlot !== a[''] && (a[''] && (a[''].__wxSlotChildren = t.childNodes), v(t, a, i, true), t.__singleSlot && (t.__singleSlot.__wxSlotChildren = []), t.__singleSlot = a[''] || null) : (v(t, a, i, false), t.__slots = a);
    }
};
var v = function (e, t, i, n) {
    var a = e.childNodes;
    var r = 0;
    if (n) {
        var o = t[''];
        if (o)
            for (; r < a.length; r++)
                I(o, a[r], null, false, false, false, -1);
        else if (o = i[''])
            for (; r < a.length; r++)
                I(o, null, a[r], true, false, false, r);
    } else {
        var s = function (e) {
            for (var n = 0; n < e.length; n++) {
                var a = e[n];
                var r = a.__slot || '';
                var o = t[r];
                if (o) {
                    I(o, a, null, false, true, true);
                } else {
                    (o = i[r]) && I(o, null, a, true, true, true);
                }
                a.__inheritSlots && s(a.childNodes);
            }
        };
        s(a);
    }
};
var w = function (e, t, i, n) {
    for (var a = function (e, t, n) {
            var r = e.childNodes;
            var o = 0;
            for (t && (o = r.indexOf(t) + (n ? 0 : 1)); o < r.length; o++) {
                var s = r[o];
                if (s.__slot === i)
                    return s;
                if (s.__inheritSlots) {
                    var l = a(s, null, false);
                    if (l)
                        return l;
                }
            }
            return null;
        }; t !== e; t = t.parentNode) {
        var r = a(t.parentNode, t, n);
        if (r)
            return r;
        n = false;
    }
    return null;
};
var m = function (e) {
    for (var t = e.parentNode; t && t.__inheritSlots;)
        t = t.parentNode;
    if (t && undefined === t.__singleSlot) {
        var i = e.__slot || '';
        var n = t.__slots[i];
        if (n) {
            var a = w(t, e, i, false);
            I(n, e, a, false, true, true);
        } else
            (n = e.__wxSlotParent) && I(n, null, e, true, true, true);
    }
};
var I = function (e, t, i, n, a, r, o) {
    n && (i.__wxSlotParent = null);
    a && (o = e.__wxSlotChildren.indexOf(i));
    if (t) {
        var s = t.__wxSlotParent;
        t.__wxSlotParent = e;
        if (s && r) {
            var l = s.__wxSlotChildren.indexOf(t);
            s.__wxSlotChildren.splice(l, 1);
            s === e && l < o && o--;
        }
    }
    var A = false;
    (t && t.__backendExtracted || i && i.__backendExtracted) && (A = true);
    A || function (e, t, i, n, a) {
        var r = e;
        if (r instanceof Element) {
            for (; r.__virtual;) {
                var o = r.__wxSlotParent;
                if (!o) {
                    r = null;
                    break;
                }
                if (t && !i) {
                    var s = o.__wxSlotChildren.indexOf(r);
                    i = o.__wxSlotChildren[s + 1];
                }
                r = o;
            }
            r instanceof Element && (r = r.__domElement);
        }
        if (r) {
            var l = n;
            var d = null;
            var A = null;
            if (t)
                if (t.__virtual) {
                    var h = document.createDocumentFragment();
                    var u = function (e) {
                        for (var t = 0; t < e.__wxSlotChildren.length; t++) {
                            var i = e.__wxSlotChildren[t];
                            i.__virtual ? u(i) : h.appendChild(i.__domElement);
                        }
                    };
                    u(t);
                    d = h;
                } else
                    d = t.__domElement;
            if (i)
                if (i.__virtual) {
                    var p = e;
                    var g = 0;
                    if (n) {
                        var _ = function (e) {
                            for (var t = 0; t < e.__wxSlotChildren.length; t++) {
                                var i = e.__wxSlotChildren[t];
                                i.__virtual ? _(i) : r.removeChild(i.__domElement);
                            }
                        };
                        _(i);
                        l = false;
                        g = a + 1;
                    } else
                        p = i.__wxSlotParent, g = i === t ? a : p.__wxSlotChildren.indexOf(i);
                    if (t) {
                        var f = function (e, t) {
                            for (; t < e.__wxSlotChildren.length; t++) {
                                var i = e.__wxSlotChildren[t];
                                if (!i.__virtual)
                                    return i;
                                var n = f(i, 0);
                                if (n)
                                    return n;
                            }
                        };
                        i = null;
                        for (var v = p; !(i = f(v, g)) && v.__virtual; v = v.__wxSlotParent)
                            g = v.__wxSlotParent.__wxSlotChildren.indexOf(v) + 1;
                        i && (A = i.__domElement);
                    }
                } else
                    A = i.__domElement;
            if (l) {
                d ? r.replaceChild(d, A) : r.removeChild(A);
            } else {
                if (d) {
                    A ? r.insertBefore(d, A) : r.appendChild(d);
                };
            }
        } else if (t && t.__wxSlotParent) {
            var w = function (e) {
                if (e.__virtual)
                    for (var t = 0; t < e.__wxSlotChildren.length; t++)
                        w(e.__wxSlotChildren[t]);
                else {
                    var i = e.__domElement;
                    i && i.parentNode && i.parentNode.removeChild(i);
                }
            };
            w(t);
        }
    }(e, t, i, n, o);
    if (globalOptions.writeExtraInfoToAttr) {
        n && i instanceof Element && i.__domElement && i.__domElement.removeAttribute('exparser:info-in-slot-of');
        t instanceof Element && t.__domElement && (undefined !== e.__slotName && e.ownerShadowRoot ? t.__domElement.setAttribute('exparser:info-in-slot-of', e.ownerShadowRoot.__wxHost.__componentInstanceId) : t.__domElement.removeAttribute('exparser:info-in-slot-of'));
    };
    a && (-1 === o && (o = e.__wxSlotChildren.length), t ? e.__wxSlotChildren.splice(o, n ? 1 : 0, t) : e.__wxSlotChildren.splice(o, n ? 1 : 0));
};
var b = function (e, t) {
    var i = e.childNodes;
    for (var n = 0; n < i.length; n++) {
        var a = i[n];
        t(e, a);
        a.__inheritSlots && b(a, t);
    }
};
var C = Element.insertChildToElement = function (e, t, i, n) {
    if (undefined !== e.__slotName)
        return false;
    var a = i >= 0 ? e.childNodes[i] : null;
    n && t === a && (n = false);
    var o = null;
    var s = e;
    var l = e;
    if (t) {
        o = t.parentNode;
        t.parentNode = e;
        var d = e.__subtreeObserversCount;
        if (o) {
            var A = o.childNodes.indexOf(t);
            o.childNodes.splice(A, 1);
            o === e && A < i && i--;
            d -= o.__subtreeObserversCount;
        }
        d && Observer._updateSubtreeCaches(t, d);
    }
    for (var h = e; h && h.__inheritSlots;)
        h = h.parentNode;
    var v = !h || !h.__slots;
    var m = !v || e.__inheritSlots;
    for (var C = o; C && C.__inheritSlots;)
        C = C.parentNode;
    var y = !C || !C.__slots;
    var E = !y || o && o.__inheritSlots;
    if (v) {
        undefined !== e.__singleSlot && (s = l = e.__singleSlot);
        if (s) {
            I(s, t, a, n, m, E, i);
        } else {
            t && t.__wxSlotParent && I(t.__wxSlotParent, null, t, true, E, false);
        }
        t && !y && t.__inheritSlots && b(t, function (e, t) {
            I(e, t, null, false, true, true, -1);
        });
    } else {
        var x = '';
        if (t) {
            x = t.__slot || '';
            s = h.__slots[x] || null;
        };
        a && (l = h.__slots[a.__slot || ''] || null);
        a && l && n && I(l, null, a, n, true, false);
        if (t)
            if (s) {
                var B = a ? w(h, a, x, !n) : w(h, e, x, false);
                I(s, t, B, false, true, E);
            } else
                t.__wxSlotParent && I(t.__wxSlotParent, null, t, true, E, false);
        a && n && a.__inheritSlots && b(a, function (e, t) {
            I(e, t, null, false, true, true, -1);
        });
        t && t.__inheritSlots && b(t, function (t, i) {
            var r = i.__slot || '';
            var o = h.__slots[r] || null;
            if (o) {
                var s = a ? w(h, a, r, !n) : w(h, e, r, false);
                I(o, i, s, false, true, true);
            } else
                i.__wxSlotParent && I(i.__wxSlotParent, null, i, true, true, false);
        });
    }
    if (n) {
        e.__subtreeObserversCount && Observer._updateSubtreeCaches(a, -e.__subtreeObserversCount);
        a.parentNode = null;
    };
    -1 === i && (i = e.childNodes.length);
    t ? e.childNodes.splice(i, n ? 1 : 0, t) : e.childNodes.splice(i, n ? 1 : 0);
    if (e.ownerShadowRoot) {
        n && _(a, e.ownerShadowRoot);
        t && _(t, e.ownerShadowRoot);
        f(e.ownerShadowRoot);
    };
    if (n) {
        p(a);
        g(e, 'remove', a);
    };
    if (t) {
        o ? function (e) {
            if (!e.__attached)
                return u(e);
            var t = function (e) {
                if (e instanceof Element) {
                    var i = e.childNodes;
                    for (var n = 0; n < i.length; n++)
                        t(i[n]);
                    e.shadowRoot instanceof Element && t(e.shadowRoot);
                    e.__lifeTimeFuncs && e.__lifeTimeFuncs.moved.call(e.__methodCaller, [], e);
                    e.__relationHandler && e.__relationHandler('moved');
                }
            };
            t(e);
        }(t) : u(t);
        o === e ? g(e, 'move', t) : (o && g(o, 'remove', t), g(e, 'add', t));
    };
    return true;
};
var y = function (e, t, i, n) {
    if (t && e.ownerShadowRoot !== t.ownerShadowRoot)
        throw new Error('Cannot move the node from one shadow tree to another shadow tree.');
    var a = n ? i : t;
    var r = -1;
    return i && (r = e.childNodes.indexOf(i)) < 0 ? null : C(e, t, r, n) ? a : null;
};
Element._attachShadowRoot = function (e) {
    var t = e.__wxHost;
    t.__wxSlotChildren = [e];
    I(t, e, null, false, false, 0);
    e.__hostAssociated = true;
    f(e);
};
Element.appendChild = function (e, t) {
    return y(e, t, null, false);
};
Element.insertBefore = function (e, t, i) {
    return y(e, t, i, false);
};
Element.removeChild = function (e, t) {
    return y(e, null, t, true);
};
Element.replaceChild = function (e, t, i) {
    return y(e, t, i, true);
};
Element.prototype.appendChild = function (e) {
    return y(this, e, null, false);
};
Element.prototype.insertBefore = function (e, t) {
    return y(this, e, t, false);
};
Element.prototype.removeChild = function (e) {
    return y(this, null, e, true);
};
Element.prototype.replaceChild = function (e, t) {
    return y(this, e, t, true);
};
var E = function (e) {
    var t = true;
    var i = true;
    if (e instanceof Element) {
        e.__backendExtracted = true;
        e.$$ = e.__domElement = null;
        i = false;
    } else {
        if (e instanceof TextNode) {
            e.__backendExtracted = true;
            e.$$ = e.__domElement = null;
            t = false;
            i = false;
        };
    }
    if (t && e.__wxSlotChildren)
        for (var n = 0; n < e.__wxSlotChildren.length; n++) {
            var a = e.__wxSlotChildren[n];
            E(a);
        }
    else if (i && e.childNodes)
        for (var r = 0; r < e.childNodes.length; r++)
            E(e.childNodes[r]);
};
Element.extractBackend = function (e) {
    if (e.__backendExtracted)
        return null;
    var t = e.__domElement;
    E(e);
    return t;
};
var x = function (e, t, i) {
    var n = e instanceof Element && e.__virtual;
    var a = t ? t.childNodes[i] : undefined;
    var r = true;
    var o = true;
    var d = null;
    if (e instanceof Element) {
        e.__backendExtracted = false;
        n || (d = function (e, t) {
            if (!t) {
                var i = FakeDomElement.toElement(e.__domElement);
                e.$$ = e.__domElement = i;
                return i;
            }
            if (e.__domElement.tagName.toUpperCase() === t.tagName)
                e.$$ = e.__domElement = t;
            else {
                var n = FakeDomElement.toElement(e.__domElement);
                e.$$ = e.__domElement = n;
                t.parentNode.replaceChild(n, t);
            }
            return null;
        }(e, a));
        o = false;
    } else if (e instanceof TextNode)
        e.__backendExtracted = false, d = function (e, t) {
            if (!t) {
                var i = document.createTextNode(e.textContent);
                e.$$ = e.__domElement = i;
                return i;
            }
            if (undefined !== t.textContent) {
                t.textContent = e.textContent;
                e.$$ = e.__domElement = t;
            } else {
                var n = document.createTextNode(e.textContent);
                e.$$ = e.__domElement = n;
                t.parentNode.replaceChild(n, t);
            }
            return null;
        }(e, a), r = false, o = false;
    else {
        var A = document.createDocumentFragment();
        A.__wxSlotChildren = e.__wxSlotChildren;
        if (a && a.childNodes)
            for (; a.childNodes.length;)
                A.appendChild(a.childNodes[0]);
        d = function (e, t) {
            if (!t)
                return e;
            for (; e.childNodes && t.childNodes && t.childNodes.length;)
                e.appendChild(t.childNodes[0]);
            t.parentNode.replaceChild(e, t);
            return null;
        }(e, a);
        e = A;
    }
    if (d) {
        t.appendChild(d);
        a = d;
    } else {
        a = t ? t.childNodes[i] : undefined;
    }
    var h = 0;
    if (r && e.__wxSlotChildren)
        for (var u = 0; u < e.__wxSlotChildren.length; u++) {
            var p = e.__wxSlotChildren[u];
            if (n)
                i = x(p, t, i);
            else if (p.asShadowRoot) {
                for (var g = 0; g < p.asShadowRoot.childNodes.length; g++)
                    h = x(p.asShadowRoot.childNodes[g], a, h);
                e.shadowRoot = a;
                a.__wxHost = e;
                e.__wxSlotChildren[0] = a;
                a.__wxSlotParent = e;
                e.__singleSlot === p && (e.__singleSlot = a);
            } else
                h = x(p, a, h);
        }
    else if (o && e.childNodes)
        for (var _ = 0; _ < e.childNodes.length; _++)
            h = x(e.childNodes[_], a, h);
    if (n)
        return i;
    for (; a.childNodes.length > h;)
        a.removeChild(a.childNodes[h]);
    return i + 1;
};
Element.injectBackend = function (e, t) {
    if (e.__backendExtracted) {
        var i = document.createDocumentFragment();
        i.appendChild(t);
        x(e, i, 0);
    }
};
Element.prototype.triggerEvent = function (e, t, i) {
    Event.triggerEvent(this, e, t, i);
};
Element.prototype.dispatchEvent = function (e) {
    Event.dispatchEvent(this, e);
};
Element.prototype.addListener = function (e, t, i) {
    Event.addListenerToElement(this, e, t, i);
};
Element.prototype.removeListener = function (e, t, i) {
    Event.removeListenerFromElement(this, e, t, i);
};
Element.setMethodCaller = function (e, t) {
    e.__methodCaller = t;
};
Element.getMethodCaller = function (e) {
    return e.__methodCaller;
};
Element.prototype.getAttribute = function (e) {
    if (!this.__attributes)
        return null;
    var t = this.__attributes[e];
    return undefined === t ? null : t;
};
Element.prototype.setAttribute = function (e, t) {
    this.__attributes || (this.__attributes = Object.create(null));
    t = String(t);
    this.__attributes[e] = t;
    this.__domElement && this.__domElement.setAttribute(e, t);
};
Element.prototype.removeAttribute = function (e) {
    if (this.__attributes) {
        delete this.__attributes[e];
        this.__domElement && this.__domElement.removeAttribute(e);
    };
};
Element.prototype.setMark = function (e, t) {
    this.__marks || (this.__marks = {});
    this.__marks[e] = t;
};
Element.prototype.collectMarks = function () {
    var e = {};
    for (var t = this; t; t = t.parentNode)
        if (t.__marks)
            for (var i in t.__marks)
                hasOwnProperty.call(e, i) || (e[i] = t.__marks[i]);
    return e;
};
var B = Element.prototype.setNodeStyle = function (e, t) {
    this.__styleSegments[t || 0] = e;
    var i = this.__styleSegments.join(';');
    if (this.__domElement && this.ownerShadowRoot && this.ownerShadowRoot.__childrenPropsFilter) {
        var n = this.ownerShadowRoot.__childrenPropsFilter.style;
        if ('function' == typeof n) {
            this.__domElement.setAttribute('style', n.call(this, i));
            return;
        }
    }
    this.__domElement && this.__domElement.setAttribute('style', i);
    (this.__propObservers && !this.__propObservers.empty || this.__subtreeObserversCount) && Observer._callObservers(this, '__propObservers', {
        type: 'properties',
        target: this,
        propertyName: 'style'
    });
};
Element.replaceDocumentElement = function (e, t) {
    if (!e.__attached) {
        t.parentNode.replaceChild(e.__domElement, t);
        u(e);
    };
};
Element.pretendAttached = function (e) {
    e.__attached || u(e);
};
Element.pretendDetached = function (e) {
    e.__attached && p(e);
};
Element.isAttached = function (e) {
    return e.__attached;
};
Element.setSlotName = function (e, t) {
    t = null == t ? '' : String(t);
    if (undefined === e.__slotName) {
        if (0 !== e.childNodes.length || 0 !== e.__wxSlotChildren.length)
            return;
        e.__wxSlotChildren = [];
    }
    e.__slotName = t;
    e.ownerShadowRoot && (e.ownerShadowRoot.__slotCacheDirty = true, f(e.ownerShadowRoot));
};
Element.setInheritSlots = function (e) {
    if (!(e.__singleSlot || e.__slots || e.__wxSlotChildren.length)) {
        e.__wxSlotChildren = [];
        e.__inheritSlots = true;
    };
};
Element.getInheritSlots = function (e) {
    return e.__inheritSlots;
};
var S = function (e, t) {
    var i = e.match(/^(#[_a-zA-Z][-_a-zA-Z0-9:]*|)((?:\.-?[_a-zA-Z][-_a-zA-Z0-9]*)+|)$/);
    if (!i)
        return null;
    var n = i[1].slice(1);
    var a = i[2].split('.');
    a.shift();
    return n || a.length ? {
        id: n,
        classes: a,
        relation: t || ''
    } : null;
};
var Q = Element.parseSelector = function (e) {
    var t = String(e || '').split(',');
    var i = [];
    var n = false;
    for (var a = 0; a < t.length; a++) {
        var r = t[a].split(/( |\t|>+)/g);
        var o = [];
        var s = '';
        for (var l = 0; l < r.length; l++) {
            var d = r[l];
            if (d && ' ' !== d && '\t' !== d)
                if ('>' !== d[0]) {
                    var A = S(d, s);
                    s = '';
                    if (!A)
                        break;
                    o.push(A);
                } else {
                    if ('' !== s)
                        break;
                    s = d;
                    '>>>' === d && (n = true);
                }
        }
        if (l === r.length) {
            o.length && i.push(o);
        };
    }
    return i.length ? {
        crossShadow: n,
        union: i
    } : null;
};
var k = function (e, t, i, n, a) {
    if (t === e)
        return false;
    var r = i[n];
    var o = true;
    r.id && r.id !== t.__id && (o = false);
    var s = r.classes;
    for (var l = 0; o && l < s.length; l++)
        t.classList.contains(s[l]) || (o = false);
    if (!o && '>' === a)
        return false;
    var d = t;
    if (o && 0 === n) {
        if (null === e)
            return true;
        for (d = d.parentNode; d; d = d.parentNode)
            if (d === e)
                return true;
        if ('>>>' !== a)
            return false;
        d = t;
        o = false;
    }
    var A = o ? r.relation : a;
    do {
        if (d.parentNode) {
            d = d.parentNode;
        } else {
            if ('>>>' === A) {
                d = d.__wxHost;
            } else {
                if ('>>>' === a) {
                    o = false;
                    d = d.__wxHost;
                } else {
                    d = null;
                }
            }
        }
        d === e && (d = null);
    } while (d && !Element.isSelectable(d));
    if (!d)
        return false;
    if (o) {
        if (k(e, d, i, n - 1, A))
            return true;
        if ('>>>' !== a)
            return false;
    }
    return k(e, d, i, n, a);
};
var D = function (e, t, i) {
    if (!Element.isSelectable(i))
        return false;
    var n = e.union;
    for (var a = 0; a < n.length; a++) {
        var r = n[a];
        if (k(t, i, r, r.length - 1, '>'))
            return true;
    }
    return false;
};
var M = function (e, t, i, n, a) {
    if (D(t, i, n) && (e.push(n), a))
        return true;
    if (n.shadowRoot && t.crossShadow && ((n.ownerShadowRoot ? n.ownerShadowRoot.__wxHost.__componentOptions.domain : globalOptions.domain) === n.__componentOptions.domain && M(e, t, i, n.shadowRoot, a) && a))
        return true;
    var r = n.childNodes;
    for (var o = 0; o < r.length; o++)
        if (r[o] instanceof Element && M(e, t, i, r[o], a) && a)
            return true;
    return false;
};
Element.prototype.querySelector = function (e) {
    var t = 'object' == typeof e ? e : Q(e);
    if (!t)
        return null;
    var i = [];
    M(i, t, this, this, true);
    return i[0] || null;
};
Element.prototype.querySelectorAll = function (e) {
    var t = 'object' == typeof e ? e : Q(e);
    var i = [];
    return t ? (M(i, t, this, this, false), i) : [];
};
Element.matchSelector = function (e, t) {
    var i = 'object' == typeof e ? e : Q(e);
    return !!i && D(i, null, t);
};
Element.prototype.matchSelector = function (e, t) {
    var i = 'object' == typeof e ? e : Q(e);
    return !!i && D(i, this, t);
};
Element.isSelectable = function (e) {
    return !!e.classList;
};
module.exports = Element;
